package com.moxiao.yfjh.module.maker.controller.admin.matchMakerStringing;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.error;
import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;
import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;
import com.moxiao.yfjh.module.blind.dal.dataobject.member.BlindMemberUserDO;
import com.moxiao.yfjh.module.blind.service.member.BlindMemberUserService;
import com.moxiao.yfjh.module.maker.common.GenMakerNoCommonUtil;
import com.moxiao.yfjh.module.maker.common.MakerNotifyMessageUtils;
import com.moxiao.yfjh.module.maker.controller.admin.matchMakerStringing.vo.MatchmakerStringingCreateReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.matchMakerStringing.vo.MatchmakerStringingExcelVO;
import com.moxiao.yfjh.module.maker.controller.admin.matchMakerStringing.vo.MatchmakerStringingExportReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.matchMakerStringing.vo.MatchmakerStringingPageReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.matchMakerStringing.vo.MatchmakerStringingRespVO;
import com.moxiao.yfjh.module.maker.controller.admin.matchMakerStringing.vo.MatchmakerStringingUpdateReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.memberStringing.vo.MatchmakerMemberStringingExportReqVO;
import com.moxiao.yfjh.module.maker.convert.matchMakerStringing.MatchmakerStringingConvert;
import com.moxiao.yfjh.module.maker.dal.dataobject.matchMakerStringing.MatchmakerStringingDO;
import com.moxiao.yfjh.module.maker.dal.dataobject.matchmakerInfo.MatchmakerInfoDO;
import com.moxiao.yfjh.module.maker.dal.dataobject.memberStringing.MatchmakerMemberStringingDO;
import com.moxiao.yfjh.module.maker.service.matchMakerStringing.MatchmakerStringingService;
import com.moxiao.yfjh.module.maker.service.matchmakerInfo.MatchmakerInfoService;
import com.moxiao.yfjh.module.maker.service.memberStringing.MatchmakerMemberStringingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "管理后台 - 相亲红娘牵线记录")
@RestController
@RequestMapping("/maker/matchmaker-stringing")
@Validated
public class MatchmakerStringingController {

    @Resource
    private MatchmakerStringingService matchmakerStringingService;
    @Resource
    private BlindMemberUserService blindMemberUserService;
    @Resource
    private GenMakerNoCommonUtil genMakerNoCommonUtil;
    @Resource
    private MatchmakerInfoService matchmakerInfoService;
    @Resource
    private MakerNotifyMessageUtils messageUtils;
    @Resource
    private MatchmakerMemberStringingService matchmakerMemberStringingService;

    @PostMapping("/create")
    @Operation(summary = "创建相亲红娘牵线记录")
    @PreAuthorize("@ss.hasPermission('maker:matchmaker-stringing:create')")
    public CommonResult<Long> createMatchmakerStringing(@Valid @RequestBody MatchmakerStringingCreateReqVO createReqVO) {
        MatchmakerMemberStringingExportReqVO exportReqVO = new MatchmakerMemberStringingExportReqVO();
        exportReqVO.setMemberId(createReqVO.getActiveId());
        List<MatchmakerMemberStringingDO> matchmakerMemberStringingList = matchmakerMemberStringingService.getMatchmakerMemberStringingList(exportReqVO);
        Integer stringingNum = 0;
        if (CollectionUtils.isNotEmpty(matchmakerMemberStringingList)){
            stringingNum = matchmakerMemberStringingList.get(0).getStringingNum();
        }
        MatchmakerStringingExportReqVO matchmakerStringingExportReqVO = new MatchmakerStringingExportReqVO();
        matchmakerStringingExportReqVO.setActiveId(createReqVO.getActiveId());
        List<MatchmakerStringingDO> matchmakerStringingList = matchmakerStringingService.getMatchmakerStringingList(matchmakerStringingExportReqVO);
        if (stringingNum - matchmakerStringingList.size() <= 0){
            return error(500,"用户剩余牵线次数为0");
        }

        genMakerNoCommonUtil.addStringingNo(createReqVO);
        MatchmakerInfoDO matchmakerInfo = matchmakerInfoService.getMatchmakerInfo(createReqVO.getMatchmakerId());
        if (Objects.nonNull(matchmakerInfo)) {
            createReqVO.setMatchmakerName(matchmakerInfo.getName());
            createReqVO.setMatchmakerPhone(matchmakerInfo.getPhone());
            // 主动发起人
            BlindMemberUserDO activeuser = blindMemberUserService.getMemberUser(createReqVO.getActiveId());
            if (Objects.nonNull(activeuser)) {
                String active_user_name = activeuser.getNickname();
                // 通知
                messageUtils.createReceiveMessage(createReqVO, active_user_name);
            }
            // 被动牵线人
            BlindMemberUserDO rceiveuser = blindMemberUserService.getMemberUser(createReqVO.getReceiveId());
            if (Objects.nonNull(rceiveuser)) {
                String rceive_user_name = activeuser.getNickname();
                // 通知
                messageUtils.createMessage(createReqVO, rceive_user_name);
            }
        }
        return success(matchmakerStringingService.createMatchmakerStringing(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新相亲红娘牵线记录")
    @PreAuthorize("@ss.hasPermission('maker:matchmaker-stringing:update')")
    public CommonResult<Boolean> updateMatchmakerStringing(@Valid @RequestBody MatchmakerStringingUpdateReqVO updateReqVO) {
        matchmakerStringingService.updateMatchmakerStringing(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除相亲红娘牵线记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('maker:matchmaker-stringing:delete')")
    public CommonResult<Boolean> deleteMatchmakerStringing(@RequestParam("id") Long id) {
        matchmakerStringingService.deleteMatchmakerStringing(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得相亲红娘牵线记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('maker:matchmaker-stringing:query')")
    public CommonResult<MatchmakerStringingRespVO> getMatchmakerStringing(@RequestParam("id") Long id) {
        MatchmakerStringingDO matchmakerStringing = matchmakerStringingService.getMatchmakerStringing(id);
        MatchmakerStringingRespVO vo = MatchmakerStringingConvert.INSTANCE.convert(matchmakerStringing);
        if (Objects.nonNull(vo)) {
            if (vo.getManId() != null) {
                BlindMemberUserDO manUser = blindMemberUserService.getMemberUser(vo.getManId());
                if (Objects.nonNull(manUser)) {
                    vo.setManName(manUser.getNickname());
                }
            }
            if (vo.getWomanId() != null) {
                BlindMemberUserDO womanUser = blindMemberUserService.getMemberUser(vo.getWomanId());
                if (Objects.nonNull(womanUser)) {
                    vo.setWomanName(womanUser.getNickname());
                }
            }
        }
        return success(vo);
    }

    @GetMapping("/list")
    @Operation(summary = "获得相亲红娘牵线记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('maker:matchmaker-stringing:query')")
    public CommonResult<List<MatchmakerStringingRespVO>> getMatchmakerStringingList(@RequestParam("ids") Collection<Long> ids) {
        List<MatchmakerStringingDO> list = matchmakerStringingService.getMatchmakerStringingList(ids);
        return success(MatchmakerStringingConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得相亲红娘牵线记录分页")
    @PreAuthorize("@ss.hasPermission('maker:matchmaker-stringing:query')")
    public CommonResult<PageResult<MatchmakerStringingRespVO>> getMatchmakerStringingPage(@Valid MatchmakerStringingPageReqVO pageVO) {
        PageResult<MatchmakerStringingDO> pageResult = matchmakerStringingService.getMatchmakerStringingPage(pageVO);
        PageResult<MatchmakerStringingRespVO> page = MatchmakerStringingConvert.INSTANCE.convertPage(pageResult);
        page.getList().forEach(item -> {
            if (item.getManId() != null) {
                BlindMemberUserDO manUser = blindMemberUserService.getMemberUser(item.getManId());
                if (Objects.nonNull(manUser)) {
                    item.setManName(manUser.getNickname());
                }
            }
            if (item.getWomanId() != null) {
                BlindMemberUserDO womanUser = blindMemberUserService.getMemberUser(item.getWomanId());
                if (Objects.nonNull(womanUser)) {
                    item.setWomanName(womanUser.getNickname());
                }
            }
        });
        return success(page);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出相亲红娘牵线记录 Excel")
    @PreAuthorize("@ss.hasPermission('maker:matchmaker-stringing:export')")
    @OperateLog(type = EXPORT)
    public void exportMatchmakerStringingExcel(@Valid MatchmakerStringingExportReqVO exportReqVO,
                                               HttpServletResponse response) throws IOException {
        List<MatchmakerStringingDO> list = matchmakerStringingService.getMatchmakerStringingList(exportReqVO);
        // 导出 Excel
        List<MatchmakerStringingExcelVO> datas = MatchmakerStringingConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "相亲红娘牵线记录.xls", "数据", MatchmakerStringingExcelVO.class, datas);
    }

}
